const path = require('path')
const { genDir, genFile } = require('../utils')
const templates = require('./tpl')
class Create {
  constructor() {
    this._path = ''
    this._baseName = ''
    return this
  }
  async create(targetPath) {
    this._path = targetPath
    this._baseName = path.basename(targetPath)
    const dirStructure = {
      'src': {
        isDirectory: true,
        sub: [{ fileName: 'index.vue', file: templates.vueTpl(this._baseName) }],
      },
      'index': {
        isDirectory: false,
        file: templates.indexTpl(this._baseName),
        fileName: 'index.ts',
      },
      'package.json': {
        isDirectory: false,
        file: templates.packageJsonTpl(this._baseName),
        fileName: 'package.json',
      },
      '__tests__': {
        isDirectory: true,
        sub: [{ fileName: `${this._baseName}.spec.ts`, file: templates.testTpl(this._baseName) }],
      },
    }
    const result = []
    for (let dir in dirStructure) {
      const subDir = path.resolve(this._path, dir)
      dir = dirStructure[dir]
      if (dir.isDirectory) {
        result.push(
          genDir(subDir).then(r => {
            dir.sub.forEach(f => {
              result.push(genFile(f.fileName, subDir, f.file))
            })
          })
        )
      } else {
        result.push(genFile(dir.fileName, this._path, dir.file))
      }
    }
    return result
  }
}

module.exports = Create
